package services

import (
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strconv"
	"strings"
)

var angleMap = map[string][]float64{
	"E'": {67.5, 112.5},
	"NE": {22.5, 67.5},
	"N'": {337.5, 22.5},
	"NW": {292.5, 337.5},
	"W'": {247.5, 292.5},
	"SW": {202.5, 247.5},
	"S'": {157.5, 202.5},
	"SE": {112.5, 157.5},
}

// 蒲氏风力风速：1级0.3-1.5  2级1.6-3.3  3级3.4-5.4  4级5.5-7.9  5级8.0-10.7  6级10.8-13.8  7级13.9-17.1  8级17.2-20.7
var windMap = map[string][]float64{
	"1": {0.3, 1.5},
	"2": {1.6, 3.3},
	"3": {3.4, 5.4},
	"4": {5.5, 7.9},
	"5": {8.0, 10.7},
	"6": {10.8, 13.8},
	"7": {13.9, 17.1},
	"8": {17.2, 20.7},
}

var offSet = 0.0

func HandelSa(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	for i, report := range forecast.Reports {
		if i == 1 {
			for j := 2; j < 7; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j-1)
				report.Data[j][0].Value = m + "月" + d + "日"
				//timeNew := y + "年" + m + "月" + d + "日"
				//月和日补0
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				timeNew := y + "-" + m + "-" + d
				//这部分操作与上传文件有关，时间那些
				var tide models.TwcTide
				db.Where("date = ? AND station = ?", timeNew, "玉寮").First(&tide)
				if tide.Id != 0 {
					if tide.TimeHigh1 != "" {
						report.Data[j][1].Value = tide.TimeHigh1
						report.Data[j][2].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh1)/100+offSet, 2)
					} else {
						report.Data[j][1].Value = ""
						report.Data[j][2].Value = ""
					}
					if tide.TimeHigh2 != "" {
						report.Data[j][3].Value = tide.TimeHigh2
						report.Data[j][4].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh2)/100+offSet, 2)
					} else {
						report.Data[j][3].Value = ""
						report.Data[j][4].Value = ""
					}
					if tide.TimeLow1 != "" {
						report.Data[j][5].Value = tide.TimeLow1
						report.Data[j][6].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow1)/100+offSet, 2)
					} else {
						report.Data[j][5].Value = ""
						report.Data[j][6].Value = ""
					}
					if tide.TimeLow2 != "" {
						report.Data[j][7].Value = tide.TimeLow2
						report.Data[j][8].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow2)/100+offSet, 2)
					} else {
						report.Data[j][7].Value = ""
						report.Data[j][8].Value = ""
					}
				} else {
					for k := 1; k < 9; k++ {
						report.Data[j][k].Value = ""
					}
				}
			}
		} else if i == 0 {
			for j := 2; j < 7; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j-1)
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				var saForeOneList []models.SaForeOne
				timeNew := y + "-" + m + "-" + d
				if db.Where("date = ?", timeNew).Find(&saForeOneList).RowsAffected == 0 {
					continue
				}
				//如果saForeOneList中有一项的WaveValue或者WaveAngle为空，则跳过这一天
				isContinue := false
				for _, v := range saForeOneList {
					if v.WaveValue == "" || v.WaveAngle == "" {
						isContinue = true
						break
					}
				}
				if isContinue {
					continue
				}
				var waveValueArr []float64
				var waveAngleArr []string
				for _, v := range saForeOneList {
					waveValue, _ := strconv.ParseFloat(v.WaveValue, 64)
					waveAngle, _ := strconv.ParseFloat(v.WaveAngle, 64)
					waveValueArr = append(waveValueArr, waveValue)
					//waveAngleArr = append(waveAngleArr, waveAngle)
					for k, vRange := range angleMap {
						if v.WaveAngle == "" {
							break
						}
						if waveAngle >= vRange[0] && waveAngle < vRange[1] {
							waveAngleArr = append(waveAngleArr, k)
							break
						}
						if k == "N'" {
							if (waveAngle >= vRange[0] && waveAngle < 360) || (waveAngle >= 0 && waveAngle < vRange[1]) {
								waveAngleArr = append(waveAngleArr, k)
								break
							}
						}
					}
				}
				//处理第一行波高
				maxValue, minValue, maxIdx, minIdx := tools.MaxAndMinAndIndex(waveValueArr)
				maxValue = tools.Float64ToFixed(maxValue, 1)
				minValue = tools.Float64ToFixed(minValue, 1)
				if maxValue-minValue > 0.2 {
					if minIdx < maxIdx {
						bw := getDayTime(tools.IntToStr(maxIdx%24) + ":00")
						report.Data[1][j].Value = tools.Float64ToFixedStr(minValue, 1) + "，" + bw + "增大到" + tools.Float64ToFixedStr(maxValue, 1)
						report.Data[3][j].Value = tools.Float64ToFixedStr(minValue, 1) + "，" + bw + "增大到" + tools.Float64ToFixedStr(maxValue, 1)
						//report.Data[1][j].Value = tools.Float64ToFixedStr(minValue, 1) + "-" + tools.Float64ToFixedStr(maxValue, 1) + "，" + bw + "增大到" + tools.Float64ToFixedStr(maxValue, 1)
					} else {
						bw := getDayTime(tools.IntToStr(minIdx%24) + ":00")
						report.Data[1][j].Value = tools.Float64ToFixedStr(maxValue, 1) + "，" + bw + "减小到" + tools.Float64ToFixedStr(minValue, 1)
						report.Data[3][j].Value = tools.Float64ToFixedStr(maxValue, 1) + "，" + bw + "减小到" + tools.Float64ToFixedStr(minValue, 1)
						//report.Data[1][j].Value = tools.Float64ToFixedStr(maxValue, 1) + "-" + tools.Float64ToFixedStr(minValue, 1) + "，" + bw + "减小到" + tools.Float64ToFixedStr(minValue, 1)
					}
				} else {
					report.Data[1][j].Value = tools.Float64ToFixedStr(minValue, 1) + "-" + tools.Float64ToFixedStr(maxValue, 1)
					report.Data[3][j].Value = tools.Float64ToFixedStr(minValue, 1) + "-" + tools.Float64ToFixedStr(maxValue, 1)
				}
				//	第二行波向
				if waveAngleArr[0] == waveAngleArr[len(waveAngleArr)-1] {
					report.Data[2][j].Value = waveAngleArr[0]
					report.Data[4][j].Value = waveAngleArr[0]
				} else {
					report.Data[2][j].Value = waveAngleArr[0] + "转" + waveAngleArr[len(waveAngleArr)-1]
					report.Data[4][j].Value = waveAngleArr[0] + "转" + waveAngleArr[len(waveAngleArr)-1]
				}
			}
		} else {
			y, m, d := tools.GetNextDay(year, month, day, 0)
			if len(m) == 1 {
				m = "0" + m
			}
			if len(d) == 1 {
				d = "0" + d
			}
			timeNow := y + "-" + m + "-" + d
			y, m, d = tools.GetNextDay(year, month, day, 1)
			if len(m) == 1 {
				m = "0" + m
			}
			if len(d) == 1 {
				d = "0" + d
			}
			timeNext := y + "-" + m + "-" + d
			var saForeOneList []models.SaForeOne
			//	获取time的17时开始奇数小时的数据直到第二天的15时
			if db.Where("date = ? AND time IN ('17:00','19:00','21:00','23:00')", timeNow).Or("date = ? AND time IN ('01:00','03:00','05:00','07:00','09:00','11:00','13:00','15:00','17:00')", timeNext).Find(&saForeOneList).RowsAffected == 0 {
				continue
			}
			for idx, v := range saForeOneList {
				if v.WaveValue != "" {
					report.Data[2][idx+2].Value = tools.Float64ToStr(tools.Float64ToFixed(tools.StrToFloat64(v.WaveValue), 1))
					report.Data[5][idx+2].Value = tools.Float64ToStr(tools.Float64ToFixed(tools.StrToFloat64(v.SurgeValue), 1))
				}
				for k, vRange := range angleMap {
					if v.WaveAngle == "" {
						break
					}
					waveAngle, _ := strconv.ParseFloat(v.WaveAngle, 64)
					if waveAngle >= vRange[0] && waveAngle < vRange[1] {
						report.Data[3][idx+2].Value = k
						report.Data[6][idx+2].Value = k
						break
					}
					if k == "N'" {
						if (waveAngle >= vRange[0] && waveAngle < 360) || (waveAngle >= 0 && waveAngle < vRange[1]) {
							report.Data[3][idx+2].Value = k
							report.Data[6][idx+2].Value = k
							break
						}
					}

					//surgeAngle, _ := strconv.ParseFloat(v.SurgeAngle, 64)
					//if surgeAngle >= vRange[0] && surgeAngle < vRange[1] {
					//	report.Data[6][idx+2].Value = k
					//	break
					//}
					//if k == "N'" {
					//	if (surgeAngle >= vRange[0] && surgeAngle < 360) || (surgeAngle >= 0 && surgeAngle < vRange[1]) {
					//		report.Data[6][idx+2].Value = k
					//		break
					//	}
					//}
				}
				report.Data[4][idx+2].Value = v.Cycle
				report.Data[7][idx+2].Value = tools.Float64ToFixedStr(tools.StrToFloat64(v.SurgeAngle), 1)
				//report.Data[8][idx+2].Value = tools.Float64ToStr(tools.Float64ToFixed(tools.StrToFloat64(v.WindValue), 1))
				for k, vRange := range windMap {
					if v.WindValue == "" {
						break
					}
					//即5.6更靠近3级3.4-5.4中的5.5，则风力填入3▽4
					windValue, _ := strconv.ParseFloat(v.WindValue, 64)
					if windValue >= vRange[0] && windValue <= vRange[1] {
						if windValue-vRange[0] < vRange[1]-windValue {
							report.Data[8][idx+2].Value = tools.IntToStr(tools.StrToInt(k)-1) + "▽" + k
						} else {
							report.Data[8][idx+2].Value = tools.IntToStr(tools.StrToInt(k)-1) + "-" + k + "▽" + tools.IntToStr(tools.StrToInt(k)+1)
						}
						break
					}
				}
				for k, vRange := range angleMap {
					if v.WindAngle == "" {
						break
					}
					waveAngle, _ := strconv.ParseFloat(v.WindAngle, 64)
					if waveAngle >= vRange[0] && waveAngle < vRange[1] {
						report.Data[9][idx+2].Value = k
						break
					}
					if k == "N'" {
						if (waveAngle >= vRange[0] && waveAngle < 360) || (waveAngle >= 0 && waveAngle < vRange[1]) {
							report.Data[9][idx+2].Value = k
							break
						}
					}
				}
			}
		}
	}
}

func HandelSz(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	for i, report := range forecast.Reports {
		if i == 0 {
			for j := 2; j < 9; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j-1)
				report.Data[j][0].Value = m + "月" + d + "日"
				//timeNew := y + "年" + m + "月" + d + "日"
				//月和日补0
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				timeNew := y + "-" + m + "-" + d
				//这部分操作与上传文件有关，时间那些
				var tide models.TwcTide
				db.Where("date = ? AND station = ?", timeNew, "玉寮").First(&tide)
				if tide.Id != 0 {
					if tide.TimeHigh1 != "" {
						report.Data[j][1].Value = tide.TimeHigh1
						report.Data[j][2].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh1)/100+offSet, 2)
					} else {
						report.Data[j][1].Value = ""
						report.Data[j][2].Value = ""
					}
					if tide.TimeHigh2 != "" {
						report.Data[j][3].Value = tide.TimeHigh2
						report.Data[j][4].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh2)/100+offSet, 2)
					} else {
						report.Data[j][3].Value = ""
						report.Data[j][4].Value = ""
					}
					if tide.TimeLow1 != "" {
						report.Data[j][5].Value = tide.TimeLow1
						report.Data[j][6].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow1)/100+offSet, 2)
					} else {
						report.Data[j][5].Value = ""
						report.Data[j][6].Value = ""
					}
					if tide.TimeLow2 != "" {
						report.Data[j][7].Value = tide.TimeLow2
						report.Data[j][8].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow2)/100+offSet, 2)
					} else {
						report.Data[j][7].Value = ""
						report.Data[j][8].Value = ""
					}
				} else {
					for k := 1; k < 9; k++ {
						report.Data[j][k].Value = ""
					}
				}
			}
		} else {
			for j := 1; j < 8; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j)
				report.Data[j][0].Value = m + "月" + d + "日"
				if j > 5 {
					continue
				}
				//月和日补0
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				var saForeOneList []models.SaForeOne
				timeNew := y + "-" + m + "-" + d
				//db.Where("date = ?", time).Find(&saForeOne)
				if db.Where("date = ?", timeNew).Find(&saForeOneList).RowsAffected == 0 {
					continue
				}
				isContinue := false
				for _, v := range saForeOneList {
					if v.WaveValue == "" || v.WaveAngle == "" {
						isContinue = true
						break
					}
				}
				if isContinue {
					continue
				}
				var waveValueArr []float64
				var waveAngleArr []string
				for _, v := range saForeOneList {
					waveValue, _ := strconv.ParseFloat(v.WaveValue, 64)
					waveAngle, _ := strconv.ParseFloat(v.WaveAngle, 64)
					waveValueArr = append(waveValueArr, waveValue)
					//waveAngleArr = append(waveAngleArr, waveAngle)
					for k, vRange := range angleMap {
						if v.WaveAngle == "" {
							break
						}
						if waveAngle >= vRange[0] && waveAngle < vRange[1] {
							waveAngleArr = append(waveAngleArr, k)
							break
						}
						if k == "N'" {
							if (waveAngle >= vRange[0] && waveAngle < 360) || (waveAngle >= 0 && waveAngle < vRange[1]) {
								waveAngleArr = append(waveAngleArr, k)
								break
							}
						}
					}
				}
				//处理第一行波高
				maxValue, minValue, maxIdx, minIdx := tools.MaxAndMinAndIndex(waveValueArr)
				maxValue = tools.Float64ToFixed(maxValue, 1)
				minValue = tools.Float64ToFixed(minValue, 1)
				if maxValue-minValue > 0.2 {
					if minIdx < maxIdx {
						bw := getDayTime(tools.IntToStr(maxIdx%24) + ":00")
						report.Data[j][1].Value = tools.Float64ToFixedStr(minValue, 1) + "，" + bw + "增大到" + tools.Float64ToFixedStr(maxValue, 1)

						//report.Data[j][1].Value = tools.Float64ToFixedStr(minValue, 1) + "-" + tools.Float64ToFixedStr(maxValue, 1) + "，" + bw + "增大到" + tools.Float64ToFixedStr(maxValue, 1)
					} else {
						bw := getDayTime(tools.IntToStr(minIdx%24) + ":00")
						report.Data[j][1].Value = tools.Float64ToFixedStr(maxValue, 1) + "，" + bw + "减小到" + tools.Float64ToFixedStr(minValue, 1)

						//report.Data[j][1].Value = tools.Float64ToFixedStr(maxValue, 1) + "-" + tools.Float64ToFixedStr(minValue, 1) + "，" + bw + "减小到" + tools.Float64ToFixedStr(minValue, 1)
					}
				} else {
					report.Data[j][1].Value = tools.Float64ToFixedStr(minValue, 1) + "-" + tools.Float64ToFixedStr(maxValue, 1)
				}
				//	第二行波向
				//if waveAngleArr[0] == waveAngleArr[len(waveAngleArr)-1] {
				//	report.Data[j][2].Value = waveAngleArr[0]
				//} else {
				//	report.Data[j][2].Value = waveAngleArr[0] + "转" + waveAngleArr[len(waveAngleArr)-1]
				//}
			}
		}
	}
}
